package cc.mrbird.febs.mall.controller;

import cc.mrbird.febs.common.annotation.ControllerEndpoint;
import cc.mrbird.febs.common.utils.FebsUtil;
import cc.mrbird.febs.common.entity.FebsConstant;
import cc.mrbird.febs.common.controller.BaseController;
import cc.mrbird.febs.common.entity.FebsResponse;
import cc.mrbird.febs.common.entity.QueryRequest;
import cc.mrbird.febs.mall.entity.Banner;
import cc.mrbird.febs.mall.service.IBannerService;
import com.wuwenze.poi.ExcelKit;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 *  Controller
 *
 * @author Mr.Yang
 * @date 2022-09-08 20:21:08
 */
@Slf4j
@Validated
@Controller
public class BannerController extends BaseController {

    @Autowired
    private IBannerService bannerService;

    @GetMapping(FebsConstant.VIEW_PREFIX + "banner")
    public String bannerIndex(){
        return FebsUtil.view("banner/banner");
    }

    @GetMapping("banner")
    @ResponseBody
    @RequiresPermissions("banner:list")
    public FebsResponse getAllBanners(Banner banner) {
        return new FebsResponse().success().data(bannerService.findBanners(banner));
    }

    @GetMapping("banner/list")
    @ResponseBody
    @RequiresPermissions("banner:list")
    public FebsResponse bannerList(QueryRequest request, Banner banner) {
        Map<String, Object> dataTable = getDataTable(this.bannerService.findBanners(request, banner));
        return new FebsResponse().success().data(dataTable);
    }

    @ControllerEndpoint(operation = "新增Banner", exceptionMessage = "新增Banner失败")
    @PostMapping("banner")
    @ResponseBody
    @RequiresPermissions("banner:add")
    public FebsResponse addBanner(@Valid Banner banner) {
        this.bannerService.createBanner(banner);
        return new FebsResponse().success();
    }

    @ControllerEndpoint(operation = "删除Banner", exceptionMessage = "删除Banner失败")
    @GetMapping("banner/delete")
    @ResponseBody
    @RequiresPermissions("banner:delete")
    public FebsResponse deleteBanner(Banner banner) {
        this.bannerService.deleteBanner(banner);
        return new FebsResponse().success();
    }

    @ControllerEndpoint(operation = "修改Banner", exceptionMessage = "修改Banner失败")
    @PostMapping("banner/update")
    @ResponseBody
    @RequiresPermissions("banner:update")
    public FebsResponse updateBanner(Banner banner) {
        this.bannerService.updateBanner(banner);
        return new FebsResponse().success();
    }

    @ControllerEndpoint(operation = "修改Banner", exceptionMessage = "导出Excel失败")
    @PostMapping("banner/excel")
    @ResponseBody
    @RequiresPermissions("banner:export")
    public void export(QueryRequest queryRequest, Banner banner, HttpServletResponse response) {
        List<Banner> banners = this.bannerService.findBanners(queryRequest, banner).getRecords();
        ExcelKit.$Export(Banner.class, response).downXlsx(banners, false);
    }
}
